package com.stu.service.impl;

import com.alibaba.fastjson.JSON;
import com.stu.bean.CategoryEntity;
import com.stu.service.DemoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @ClassName DemoServiceImpl.java
 * @createTime 2021年09月08日 15:25:00
 */
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
    @Override
    public LocalDateTime localDateTime() {
        log.info("{}", StringUtils.center("-", 51, "*"));
        try {
            TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(1));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return LocalDate.now().atTime(LocalTime.now());
    }

    @Override
    @SneakyThrows
    @Cacheable(cacheNames = "all", key = "'ks'")
    public List<CategoryEntity> cacheKey() {
        System.out.println("读取文件了");
        final String s = FileUtils.readFileToString(ResourceUtils.getFile("classpath:test.json"),
                StandardCharsets.UTF_8);
        final List<CategoryEntity> list = JSON.parseArray(s, CategoryEntity.class);
        return list;
    }
}
